/* 
 * Copyright (c) 2021, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.boot.component.uploader;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import com.cory.boot.utils.CommonData;
import com.cory.utils.file.FileUtils;
import com.cory.utils.lang.DateUtils;
import com.cory.utils.lang.StringUtils;

/**
 * @author xiongcong
 * @date 2021-05-30
 */
public class UploaderUtils {
    
    public static String getUrl(String fileId) {
        if (StringUtils.isBlank(fileId) || fileId.startsWith("http")) {
            return fileId;
        }
        
        String domain = StringUtils.defaultIfBlank(CommonData.getEnvConfig("resource.domain"), UploaderConfig.domain);
        return StringUtils.defaultString(domain, "") + fileId;
    }
    
    public static String getUploaderUrl() {
        return StringUtils.defaultIfBlank(UploaderConfig.domain, "") + "/uploader/file";
    }

    public static String getUEditorUploaderUrl() {
        return StringUtils.defaultIfBlank(UploaderConfig.domain, "") + "/baidu/editor/upload";
    }

    public static String getDownLoadUrl() {
        return StringUtils.defaultIfBlank(UploaderConfig.domain, "") + "/download/file";
    }
    
    public static File getFile(String fileId) {
        String fullPath = UploaderConfig.resource + fileId;
        return new File(fullPath);
    }
    
    public static String getPath(String fileId) {
        String fullPath = UploaderConfig.resource + fileId;
        return fullPath;
    }
    
    public static String saveRemoteFile(String fileUrl, String suffix) {
        if(StringUtils.isEmpty(fileUrl)) {
            return null;
        }
        suffix = StringUtils.defaultIfBlank(suffix, fileUrl.substring(fileUrl.lastIndexOf(".") + 1));
        String newName = DateUtils.getDate("yyyyMMddHHmmss") + "_" + new Random().nextInt(1000) + "." + suffix;
        
        String fileId = "/resource/" + DateUtils.getYear() + "/" + DateUtils.getMonth() + "/" + newName;
        String fullPath = UploaderConfig.resource + fileId;
        FileUtils.createFile(fullPath);
        
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File f = new File(fullPath);
        try {
            urlfile = new URL(fileUrl);
            httpUrl = (HttpURLConnection) urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            bos.flush();
            bis.close();
            httpUrl.disconnect();
            
            return fileId;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileUrl;
    }
}
